<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use app\common\controller\Base;
use utils\Aes5;

/**
 * @title 用户中心
 */
class User extends Base {

    public function __construct(\think\Request $request = null) {
        parent::__construct($request);

        // 权限检查 
        $token = cookie('token');

        if (empty($token)) {
            $this->error('未登录');
        }

        $token_json = Aes5::decrypt($token, substr(md5('nariims'), 8, 16));

        $user_info = json_decode($token_json, true);

        if (!isset($user_info['id']) || empty($user_info['id'])) {
            $this->error('验证未通过');
        }

        define('MID', $user_info['id']);
    }

    /**
     * @title 用户中心首页
     */
    public function index() {



        return view();
    }

    /**
     * @title 会员积分记录
     */
    public function score_lists() {


        $lists = db('member_score')->where('member_id', MID)->order('id desc')->select();

        $this->assign('lists', $lists);

        return view();
    }

    /**
     * @title 会员签到
     */
    public function sign() {


        $first = date("w", strtotime(date("Y-m-01 00:00:00"))); //获取第一个月第一天是星期几
        $weekArr = array("日", "一", "二", "三", "四", "五", "六"); //国外第一天是星期日
        $maxDay = date('t', strtotime("" . date("Y") . "-" . date("m") . "")); //当前月份最大天数，比如 28,29,30,31

        $blankArr = [];
        for ($j = 0; $j < $first; $j++) { //日历第一行空白期
            $blankArr[] = $j;
        }

        for ($i = 0; $i < $maxDay; $i++) { //日历当前天数显示
            $z = $first + $i;
            $days[] = array("key" => $i, "key2" => $z % 7);
        }
        $nowtime = date("Y年m月d日 H:i:s") . "&nbsp;&nbsp;星期" . $weekArr[date("w")]; //当前时间和星期几
        $this->assign("nowtime", $nowtime);
        $this->assign("days", $days);
        $this->assign("first", $first);
        $this->assign("blankArr", $blankArr);
        $total = $first + count($days);

        for ($x = 0; $x < ceil($maxDay / 7) * 7 - $total; $x++) { //日历最后一行空白处
            $other[] = $x;
        }
        $this->assign("other", $other);
        $this->assign("title", "会员签到");
        return view();
    }

    /**
     * @title 签到提交
     */
    public function sign_day() {



        $data['create_time'] = strtotime(date("Y-m-d 00:00:00"));
        $data['uid'] = MID;
        $info = db('member_sign')->field("id")->where("create_time = " . $data['create_time'] . " AND uid = " . $data['uid'] . " AND status = 0")->find(); //判断是否签到过

        if (empty($info)) { //若是未签到则去签到
            $data['money'] = 20;
            $lastid = db('member_sign')->insertGetId($data);
            if ($lastid > 0) {
                $this->_addPoints("day_sign", $data['money'], MID, "每日签到获得" . $data['money'] . "积分", 1);
                echo $data['money'];
            }
        } else {
            echo -1;
        }
    }

    /**
     * @title 会员积分增加
     */
    public function _addPoints($alias, $score, $mid, $remark, $type) {

        db('member_score')->insert([
            'alias' => $alias,
            'score' => $score,
            'member_id' => $mid,
            'remark' => $remark,
            'type' => $type,
            'create_time' => time(),
        ]);

        // 收入
        if ($type == 1) {
            db('member')->where('id', MID)->setInc('score', $score);
        }
        // 支出
        if ($type == 2) {
            db('member')->where('id', MID)->setDec('score', $score);
        }
    }

}
